package grimbo.goodwinfooty;

import grimbo.goodwinfooty.processing.CSV2UntypedListProcessor;
import grimbo.goodwinfooty.processing.FootyMatrixToJSProcessor;
import grimbo.goodwinfooty.processing.FootyMatrixToSQLProcessor;
import grimbo.goodwinfooty.processing.Processor;
import grimbo.goodwinfooty.processing.UntypedList2FootyMatrixProcessor;
import grimbo.goodwinfooty.processing.XLS2CSVProcessor;
import grimbo.goodwinfooty.processing.XLS2InputStreamProcessor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodwinGenerator {
    public GoodwinGenerator() {
    }

    public Map generateFootyMatrix(Map context) throws Exception {
        List processors = new ArrayList();
        processors.add(new XLS2InputStreamProcessor());
        processors.add(new XLS2CSVProcessor());
        processors.add(new CSV2UntypedListProcessor());
        processors.add(new UntypedList2FootyMatrixProcessor());
        processors.add(new FootyMatrixToJSProcessor());
        processors.add(new FootyMatrixToSQLProcessor());
        return process(context, processors);
    }

    public Map generateUntypedList(Map context) throws Exception {
        List processors = new ArrayList();
        processors.add(new XLS2InputStreamProcessor());
        processors.add(new XLS2CSVProcessor());
        processors.add(new CSV2UntypedListProcessor());
        return process(context, processors);
    }

    public Map generateCSV(Map context) throws Exception {
        List processors = new ArrayList();
        processors.add(new XLS2InputStreamProcessor());
        processors.add(new XLS2CSVProcessor());
        return process(context, processors);
    }

    private Map process(Map context, List processors) throws Exception {
        for (int i = 0; i < processors.size(); i++) {
            Processor p = (Processor) processors.get(i);
            p.process(context);
        }
        return context;
    }

    public static void main(String[] args) throws Exception {
        if (args.length < 1) {
            System.err.println("Use:");
            System.err.println("  GoodwinGenerator <xls file>");
            System.exit(1);
        }

        GoodwinGenerator gg = new GoodwinGenerator();
        String xlsFilename = args[0];

        Map context = new HashMap();
        context.put(XLS2InputStreamProcessor.CTX_XLS_FILE_NAME, xlsFilename);
        context.put("jsFilename", "./html/footydb.js");
        context.put("sqlFilename", "./sql/data.sql");
        gg.generateFootyMatrix(context);
    }
}
